package com.sailfish.springbootdemo.dao.db7;

import com.sailfish.springbootdemo.common.DB7TableName;
import com.sailfish.springbootdemo.pojo.db7.TbFlowProcessDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface TbFlowProcessDetailDao {

    @Insert("<script>" +
            "insert into " + DB7TableName.TB_FLOW_PROCESS_DETAIL + " " +
            "( "+
            "flow_no, " +
            "flow_type, " +
            "flow_name, " +
            "flow_level, " +
            "flow_approver, " +
            "status, " +
            "sort, " +
            "remark, " +
            "create_user, " +
            "create_time, " +
            "update_user, " +
            "update_time, " +
            "flow_approver_authorize,"+
            "comments,"+
            "designer,"+
            "other_resource,"+
            "cc,"+
            "difficulty ,"+
            "equivalent_num,"+
            "vpl_finish_time,"+
            "vpl_create_user,"+
            "other_resource_files,"+
            "is_vpl_parameters_checked,"+
            "is_need_knowledge_base, "+
            "is_skip "+
            ")"+
            "values "+
            "<foreach collection=\"entities\" item=\"entity\" separator=\",\">" +
            "(#{entity.flow_no}, " +
            "#{entity.flow_type}, " +
            "#{entity.flow_name}, " +
            "#{entity.flow_level}, " +
            "#{entity.flow_approver}, " +
            "#{entity.status}, " +
            "#{entity.sort}, " +
            "#{entity.remark}, " +
            "#{entity.create_user}, " +
            "#{entity.create_time}, " +
            "#{entity.update_user}, " +
            "#{entity.update_time}," +
            "#{entity.flow_approver_authorize},"+
            "#{entity.comments},"+
            "#{entity.designer},"+
            "#{entity.other_resource},"+
            "#{entity.cc},"+
            "#{entity.difficulty},"+
            "#{entity.equivalent_num},"+
            "#{entity.vpl_finish_time},"+
            "#{entity.vpl_create_user},"+
            "#{entity.other_resource_files},"+
            "#{entity.is_vpl_parameters_checked},"+
            "#{entity.is_need_knowledge_base}, "+
            "#{entity.is_skip} "+
            ") " +
            "</foreach>"+
            "</script>"
    )
    Integer insertFlowTemplateBatch(@Param("entities")List<TbFlowProcessDetail>entities);


    @Select("<script>" +
            "select * from " + DB7TableName.TB_FLOW_PROCESS_DETAIL + " where flow_no = #{flowNo} " +
            " order by flow_level asc,sort asc"+
            "</script>"
    )
    List<TbFlowProcessDetail> getFlowDetailsByFlowNo(@Param("flowNo")String flowNo);

    @Select("<script> " +
            "SELECT * " +
            "FROM " + DB7TableName.TB_FLOW_PROCESS_DETAIL +
            "<if test='flowNoList != null and !flowNoList.isEmpty()'>" +
            " WHERE flow_no IN " +
            "<foreach collection='flowNoList' item='flowNo' open='(' separator=',' close=')'> " +
            "#{flowNo} " +
            "</foreach> " +
            "</if>" +

            " ORDER BY flow_level ASC, sort ASC " +
            "</script>")
    List<TbFlowProcessDetail> getFlowToDetailByFlowNo(@Param("flowNoList") List<String> flowNoList);

    @Select("<script>" +
            "select * from " + DB7TableName.TB_FLOW_PROCESS_DETAIL + " " +
            " order by flow_level asc,sort asc"+
            "</script>")
    List<TbFlowProcessDetail> getFlowTemplateList();

    @Delete("<script>" +
            "delete from " + DB7TableName.TB_FLOW_PROCESS_DETAIL + " where flow_no = #{flowNo} " +
            "</script>")
    Integer deleteTbFlowProcessDetailByFlowNo(String flowNo);

    @Select("<script>" +
            "select * from " + DB7TableName.TB_FLOW_PROCESS_DETAIL + " " +
            "where 1=1" +
            "   <if test='tbFlowProcessDetail.flow_no != null'>and flow_no = #{tbFlowProcessDetail.flow_no}</if>" +
            "   <if test='tbFlowProcessDetail.flow_type != null'>and flow_type = #{tbFlowProcessDetail.flow_type}</if>" +
            "   <if test='tbFlowProcessDetail.flow_level != null'>and flow_level = #{tbFlowProcessDetail.flow_level}</if>" +
            "   <if test='tbFlowProcessDetail.vpl_create_user != null'>and vpl_create_user = #{tbFlowProcessDetail.vpl_create_user}</if>" +
            "   <if test='tbFlowProcessDetail.designer != null'>and designer = #{tbFlowProcessDetail.designer}</if>" +
            " order by flow_level asc,sort asc"+
            "</script>"
    )
    List<TbFlowProcessDetail> getFlowProcessDetail(@Param("tbFlowProcessDetail")TbFlowProcessDetail tbFlowProcessDetail);

    @Update("<script>" +
            "update " + DB7TableName.TB_FLOW_PROCESS_DETAIL +
            "<set>" +
            "<if test='tbFlowProcessDetail.status != null '>" +
            "    status = #{tbFlowProcessDetail.status}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.remark != null '>" +
            "    remark = #{tbFlowProcessDetail.remark}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.update_time != null '>" +
            "    update_time = #{tbFlowProcessDetail.update_time}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.update_user != null '>" +
            "    update_user = #{tbFlowProcessDetail.update_user}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.comments != null '>" +
            "    comments = #{tbFlowProcessDetail.comments}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.designer != null '>" +
            "    designer = #{tbFlowProcessDetail.designer}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.flow_approver_authorize != null '>" +
            "    flow_approver_authorize = #{tbFlowProcessDetail.flow_approver_authorize}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.flow_approver != null '>" +
            "    flow_approver = #{tbFlowProcessDetail.flow_approver}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.other_resource != null '>" +
            "    other_resource = #{tbFlowProcessDetail.other_resource}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.cc != null '>" +
            "    cc = #{tbFlowProcessDetail.cc}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.difficulty != null '>" +
            "    difficulty = #{tbFlowProcessDetail.difficulty}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.equivalent_num != null '>" +
            "    equivalent_num = #{tbFlowProcessDetail.equivalent_num}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.vpl_finish_time != null '>" +
            "    vpl_finish_time = #{tbFlowProcessDetail.vpl_finish_time}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.vpl_create_user != null '>" +
            "    vpl_create_user = #{tbFlowProcessDetail.vpl_create_user}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.vpl_attr_excel != null '>" +
            "    vpl_attr_excel = #{tbFlowProcessDetail.vpl_attr_excel}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.vpl_model_excel != null '>" +
            "    vpl_model_excel = #{tbFlowProcessDetail.vpl_model_excel}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.max_Body_height != null '>" +
            "    max_Body_height = #{tbFlowProcessDetail.max_Body_height}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.max_Pin_length != null '>" +
            "    max_Pin_length = #{tbFlowProcessDetail.max_Pin_length}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.min_Pin_length != null '>" +
            "    min_Pin_length = #{tbFlowProcessDetail.min_Pin_length}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.other_resource_files != null '>" +
            "    other_resource_files = #{tbFlowProcessDetail.other_resource_files}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.vpl_pkg_excel != null '>" +
            "    vpl_pkg_excel = #{tbFlowProcessDetail.vpl_pkg_excel}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.is_vpl_parameters_checked != null '>" +
            "    is_vpl_parameters_checked = #{tbFlowProcessDetail.is_vpl_parameters_checked}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.is_need_knowledge_base != null '>" +
            "    is_need_knowledge_base = #{tbFlowProcessDetail.is_need_knowledge_base}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.is_skip != null '>" +
            "    is_skip = #{tbFlowProcessDetail.is_skip}," +
            "</if>" +
            "</set>" +
            "where flow_no=#{tbFlowProcessDetail.flow_no} and flow_level=#{tbFlowProcessDetail.flow_level} " +
            "</script>")
    Integer updateFlowProcessDetail(@Param("tbFlowProcessDetail")TbFlowProcessDetail tbFlowProcessDetail);

    @Update("<script>" +
            "update " + DB7TableName.TB_FLOW_PROCESS_DETAIL +
            "<set>" +
            "    flow_approver = #{now_approver}," +
            "</set>" +
            "where status!=#{status} and flow_approver=#{oldApprover} and flow_type=#{flow_type}" +
            "</script>")
    Integer updateFlowProcessDetailFromDeatil(@Param("oldApprover") String oldApprover,
                                              @Param("now_approver") String now_approver,
                                              @Param("flow_type") String flow_type,
                                              @Param("status") Integer status);

    @Update("<script>" +
            "update " + DB7TableName.TB_FLOW_PROCESS_DETAIL +
            "<set>" +
            "<if test='tbFlowProcessDetail.flow_approver != null '>" +
            "    flow_approver = #{tbFlowProcessDetail.flow_approver}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.flow_approver_authorize != null '>" +
            "    flow_approver_authorize = #{tbFlowProcessDetail.flow_approver_authorize}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.update_time != null '>" +
            "    update_time = #{tbFlowProcessDetail.update_time}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.update_user != null '>" +
            "    update_user = #{tbFlowProcessDetail.update_user}," +
            "</if>" +
            "</set>" +
            "where status!=2 and flow_no=#{tbFlowProcessDetail.flow_no} and flow_level=#{tbFlowProcessDetail.flow_level}" +
            "</script>")
    Integer updateFlowProcessDetailApprover(@Param("tbFlowProcessDetail")TbFlowProcessDetail tbFlowProcessDetail);


    @Update("<script>" +
            "update " + DB7TableName.TB_FLOW_PROCESS_DETAIL +
            "<set>" +
            "<if test='tbFlowProcessDetail.update_time != null '>" +
            "    update_time = #{tbFlowProcessDetail.update_time}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.designer != null '>" +
            "    designer = #{tbFlowProcessDetail.designer}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.vpl_create_user != null '>" +
            "    vpl_create_user = #{tbFlowProcessDetail.vpl_create_user}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.status != null '>" +
            "    status = #{tbFlowProcessDetail.status}," +
            "</if>" +
            "<if test='tbFlowProcessDetail.update_user != null '>" +
            "    update_user = #{tbFlowProcessDetail.update_user}," +
            "</if>" +
            "</set>" +
            "where flow_no=#{tbFlowProcessDetail.flow_no}" +
            "   <if test='tbFlowProcessDetail.flow_level != null'>and flow_level >= #{tbFlowProcessDetail.flow_level}</if>" +
            "</script>")
    Integer updateFlowProcessDetail2(@Param("tbFlowProcessDetail")TbFlowProcessDetail tbFlowProcessDetail);

}
